import { app, BrowserWindow, Menu, ipcMain, globalShortcut } from "electron";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const createWindow = () => {
  // 隐藏菜单栏
  Menu.setApplicationMenu(null);
  const win = new BrowserWindow({
    width: 1200,
    height: 800,
    frame: false,
    resizable: false,
    //   将此脚本(preload)附加到渲染器流程
    webPreferences: {
      preload: path.join(__dirname, "preload.mjs"),
      nodeIntegration: true, //开启true这一步很重要,目的是为了vue文件中可以引入node和electron相关的API
      contextIsolation: false, // 可以使用require方法
      enableRemoteModule: true, // 可以使用remote方法
    },
  });
  //开页面
   win.loadURL("http://192.168.32.165:5173/");
  // win.loadFile(path.resolve(__dirname, "../dist/index.html"));

  globalShortcut.register("Alt+Shift+D", () => {
    win.webContents.openDevTools({ mode: "detach" }); //开启开发者工具
  });
  //控制台
  // win.webContents.openDevTools();

  ipcMain.on("window-min", function () {
    win.minimize();
  });
  //登录窗口最大化
  ipcMain.on("window-max", function () {
    if (win.isMaximized()) {
      win.restore();
    } else {
      win.maximize();
    }
  });
  ipcMain.on("window-close", function () {
    win.close();
  });
};
app.whenReady().then(() => {
  createWindow();

  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
